package com.basics.common.utils;
/**
 * 通过经纬度获取两点之间的距离	
 *--------------------------------------------------->
 * @Author WangWei                                                                      
 * @date 2018年7月23日 下午4:04:38           
 * @version V1.0                       
 * @Email 1246518873@qq.com               
 *--------------------------------------------------->
 * @Description:  
 *
 *
 */
public class LocationUtils {
	private static double EARTH_RADIUS = 6378.137;
 
	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}
 
	/**
	 * 通过经纬度获取距离(单位：米)
	 * 
	 * @param lat1
	 * @param lng1
	 * @param lat2
	 * @param lng2
	 * @return 距离
	 */
	public static double getDistance(double lat1, double lng1, double lat2,
			double lng2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
				+ Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000d) / 10000d;
		s = s * 1000;
		return s;
	}
 

	/**
	 * 根据经纬度和半径计算经纬度范围
	 *
	 * @param raidus 单位米
	 * @return minLat, minLng, maxLat, maxLng
	 */
	public static double[] getAround(double lat, double lon, int raidus) {
	
	    Double latitude = lat;
	    Double longitude = lon;
	
	    Double degree = (24901 * 1609) / 360.0;
	    double raidusMile = raidus;
	
	    Double dpmLat = 1 / degree;
	    Double radiusLat = dpmLat * raidusMile;
	    Double minLat = latitude - radiusLat;
	    Double maxLat = latitude + radiusLat;
	
	    Double mpdLng = degree * Math.cos(latitude * (Math.PI / 180));
	    Double dpmLng = 1 / mpdLng;
	    Double radiusLng = dpmLng * raidusMile;
	    Double minLng = longitude - radiusLng;
	    Double maxLng = longitude + radiusLng;
	    return new double[]{minLat, minLng, maxLat, maxLng};
	}

	public static void main(String[] args) {
		double[] distance = getAround(34.2675560000, 108.9534750000,
				100);
		for(int x=0;x<distance.length;x++){
			System.out.println("距离" + distance[x] + "公里");
		}
	}
}
